package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Resource;
import org.millioncall.yueyoga.admin.service.ResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lisj 2014年5月24日 下午8:50:48
 */
@Controller
public class ResourceAction extends BaseAction {

	private ResourceService resourceService;

	@RequestMapping(value = "/findAllResources.jhtml")
	public String findAllResources(String name, String type, String enabled,
			Integer pageSize, Integer pageNo, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		Map<String, String> map = new HashMap<String, String>();
		int start = 0;
		int limit = 10;
		if (pageSize != null) {
			limit = pageSize;
		}
		if (pageNo != null) {
			start = (pageNo - 1) * limit;
		}
		if (StringUtils.isNotBlank(name)) {
			map.put("name", "%" + name + "%");
		}

		if (StringUtils.isNotBlank(type)) {
			map.put("type", type);
		}

		if (StringUtils.isNotBlank(enabled)) {
			map.put("enabled", enabled);
		}

		model.put("resources",
				resourceService.findAllResources(start, limit, map));
		return "resource_search";
	}

	@RequestMapping(value = "/toAddResource.jhtml")
	public String addResource(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		return "resource_add";
	}

	@RequestMapping(value = "/addResource.jhtml")
	public void addResource(String id, String name, String type, String url,
			String enabled, String description, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		String msg = "";
		Resource resource = new Resource();
		if (StringUtils.isNotBlank(id)) {
			resource.setId(Integer.valueOf(id));
		}
		if (StringUtils.isNotBlank(name)) {
			resource.setName(name);
		}
		if (StringUtils.isNotBlank(description)) {
			resource.setDescription(description);
		}
		if (StringUtils.isNotBlank(enabled)) {
			if("1".equals(enabled)){
				resource.setEnabled(true);
			}else{
				resource.setEnabled(false);
			}
		}
		if (StringUtils.isNotBlank(url)) {
			resource.setUrl(url);
		}
		// --默认
		resource.setType(1);
		if (resourceService.updateResource(resource)) {
			if (StringUtils.isNotBlank(id)) {
				msg = "{'success':true, 'msg':'资源修改成功!'}";
			} else {
				msg = "{'success':true, 'msg':'资源创建成功!'}";
			}
		} else {
			if (StringUtils.isNotBlank(id)) {
				msg = "{'success':false, 'msg':'资源修改失败!'}";
			} else {
				msg = "{'success':false, 'msg':'资源创建失败!'}";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	@RequestMapping(value = "/deleteResource.jhtml")
	public void deleteResource(String ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		super.initFrontHeader(request, model);
		String msg = "";
		if (resourceService.deleteResources(ids)) {
			msg = "{'success':true, 'msg':'资源删除成功!'}";
		} else {
			msg = "{'success':false, 'msg':'资源删除失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	
	/** 检查名称是否存在 */
	@RequestMapping(value="checkResourceName.jhtml")
	public void checkResourceName(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		String name = request.getParameter("name");
		
		Resource resource = this.resourceService.findResourceByName(name);
		if (resource != null) {
			msg = "{'success':false, 'msg':'名称已存在！'}";
		} else {
			msg = "{'success':true, 'msg':'名称可用！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
}
